import { saveAs } from 'file-saver'
import axios from 'axios'
import { getStore } from "@/config/mUtils.js";

import common from '@/api/api';
//const baseURL = process.env.VUE_APP_BASE_API
const baseURL = common.apihost;

export default {
  name(name, isDelete = true) {
    // var url = baseURL + "/common/download?fileName=" + encodeURI(name) + "&delete=" + isDelete
    // axios({
    //   method: 'get',
    //   url: url,
    //   responseType: 'blob',

    // }).then(res => {
    //   const blob = new Blob([res.data])
    //   this.saveAs(blob, decodeURI(res.headers['download-filename']))
    // })
    
    var iframe =document.createElement("iframe");
    iframe.style.display ="none";
    let urls = common.apihost+'/common/download?fileName='+name+'&delete='+isDelete;
    iframe.src = urls;
    console.log(urls)
    document.body.appendChild(iframe);
  },
  resource(resource) {
    var url = baseURL + "/common/download/resource?resource=" + encodeURI(resource);
    axios({
      method: 'get',
      url: url,
      responseType: 'blob',
    }).then(res => {
      const blob = new Blob([res.data])
      this.saveAs(blob, decodeURI(res.headers['download-filename']))
    })
  },
  zip(url, name) {
    var url = baseURL + url
    axios({
      method: 'get',
      url: url,
      responseType: 'blob',
    }).then(res => {
      const blob = new Blob([res.data], { type: 'application/zip' })
      this.saveAs(blob, name)
    })
  },
  saveAs(text, name, opts) {
    saveAs(text, name, opts);
  }
}
